extends Node2D


var killed_num = 0
var miss_killed = 0

func _ready():
	$AnimationPlayer.connect("animation_finished", self, "on_animation_finished")
	# 播放背景NPC走路（奶牛、熊）
	$AnimationPlayer.play("walk")
	__update_score()
	
	for i in range($Mouses.get_child_count()):
		var mouse = $Mouses.get_child(i)
		mouse.connect("Killed", self, "__on_killed")
		mouse.connect("MissKill", self, "__on_miss")
		
	$Timer.connect("timeout", self, "choice_mouse_action")
	$Timer.start()

func choice_mouse_action():
	var in_hole_mouses = Array()
	for i in range($Mouses.get_child_count()):
		var mouse = $Mouses.get_child(i)
		if !mouse.is_idle():
			continue
		
		if mouse.prev_animation == "HanHua":
			# 喊话完毕，进洞
			mouse.come_in()
		else:
			in_hole_mouses.append(i)
	
	# 随机选一只出洞
	var chioce_out_index = in_hole_mouses[randi() % len(in_hole_mouses)]
	var mouse = $Mouses.get_child(chioce_out_index)
	mouse.come_out()

func __update_score():
	$ScoreLabel1.text = "击杀："+str(killed_num)
	$ScoreLabel2.text = "漏杀："+str(miss_killed)
	
func __on_killed():
	killed_num += 1
	__update_score()
	
func __on_miss():
	miss_killed += 1
	__update_score()
	# 播放背景NPC失望（奶牛、熊）
	$AnimationPlayer.play("dispoint")
	
func on_animation_finished(anim_name):
	if anim_name == "dispoint":
		# 播放背景NPC走路（奶牛、熊）
		$AnimationPlayer.play("walk")
